/**
 * Created by yuming619 on 2016/11/24.
 */
angular.module('mealplan.cmtservice', [])
    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider

            .state('mealplan.servicelist', {
                url: '/servicelist',
                templateUrl: mealplan_viewurl + "/serviceitem/servicelist.html",
                controller: 'ServiceListCtrl'
            })
            .state('mealplan.servicedetail', {
                url: '/servicedetail/:serviceid',
                templateUrl: mealplan_viewurl + "/serviceitem/servicedetail.html",
                controller: 'ServiceDetailCtrl'
            })
            .state('mealplan.serviceadd', {
                url: '/service/add/',
                templateUrl: mealplan_viewurl + "/serviceitem/serviceadd.html",
                controller: 'ServiceAddCtrl'
            })
            .state('mealplan.serviceitemlist', {
                url: '/serviceitemlist/:serviceid',
                templateUrl: mealplan_viewurl + "/serviceitem/serviceitemlist.html",
                controller: 'ServiceItemListCtrl'
            })
            .state('mealplan.serviceitemdetail', {
                url: '/serviceitemdetail/:serviceitemid',
                templateUrl: mealplan_viewurl + "/serviceitem/serviceitemdetail.html",
                controller: 'ServiceItemDetailCtrl'
            })
            .state('mealplan.serviceitemadd', {
                url: '/serviceitem/add/',
                templateUrl: mealplan_viewurl + "/serviceitem/serviceitemadd.html",
                resolve: {
                    service_list: ['ServiceTopService', function (ServiceTopService) {
                        return ServiceTopService.selectbyall().$promise;
                    }]
                },
                controller: 'ServiceItemAddCtrl'
            })
        ;
    }])

    .controller('ServiceListCtrl', ['$scope', 'ServiceTopService', function ($scope, ServiceTopService) {
        ServiceTopService.selectbyall().$promise.then(function (data) {
            $scope.services = data;
            // console.log(data)
        })
        $scope.remove = function (service) {
            var index = $scope.services.indexOf(service);
            if (index > -1) {
                $scope.services.splice(index, 1);
            }
            ServiceTopService.remove(service.serviceid).$promise.then(function (data) {
                alert("删除成功！");
            });
        }
    }])
    .controller('ServiceDetailCtrl', ['$scope', '$stateParams', 'ServiceTopService', 'FileUpload', function ($scope, $stateParams, ServiceTopService, FileUpload) {
        ServiceTopService.getbyid($stateParams.serviceid).$promise.then(function (data) {
            $scope.service = data;
        })
        $scope.update = function (service) {
            ServiceTopService.update(service.serviceid, service, function (data) {
                alert("更新成功！");
                window.location.reload();
            }, function (data) {
                alert("更新失败！");
            })
        }
        $scope.uploadLogo = function (file) {
            FileUpload.uploadFileToUrl(file).then(function (data) {
                $scope.service.logo = data;
                alert("上传成功！");
            });
        }

    }])
    .controller('ServiceAddCtrl', ['$scope', '$stateParams', 'ServiceTopService', 'FileUpload', function ($scope, $stateParams, ServiceTopService, FileUpload) {

        $scope.add = function (service) {
            ServiceTopService.insert(service, function (data) {
                alert("添加成功！");
                history.go(-1);
            }, function (data) {
                alert("添加失败！");
            })
        }
        $scope.checkname = function (name) {
            ServiceTopService.selectbyname(name, function (data) {
                alert("该服务名称已存在!");
            }, function (data) {

            })
        }
        $scope.uploadLogo = function (file) {
            FileUpload.uploadFileToUrl(file).then(function (data) {
                $scope.service.logo = data;
                alert("上传成功！");
            });
        }
    }])
    .controller('ServiceItemListCtrl', ['$scope', '$stateParams', 'ServiceItemService', function ($scope, $stateParams, ServiceItemService) {
        // console.log($stateParams.serviceid);
        ServiceItemService.getbyserviceid($stateParams.serviceid).$promise.then(function (data) {
            $scope.serviceitems = data;
        })
        $scope.remove = function (serviceitem) {
            var index = $scope.serviceitems.indexOf(serviceitem);
            if (index > -1) {
                $scope.serviceitems.splice(index, 1);
            }
            ServiceItemService.remove(serviceitem.serviceitemid).$promise.then(function (data) {
                alert("删除成功！");
            });
        }
    }])
    .controller('ServiceItemDetailCtrl', ['$scope', '$stateParams', 'ServiceItemService', 'FileUpload', function ($scope, $stateParams, ServiceItemService, FileUpload) {
        ServiceItemService.getbyid($stateParams.serviceitemid).$promise.then(function (data) {
            $scope.serviceitem = data;
        })
        $scope.update = function (serviceitem) {
            ServiceItemService.update(serviceitem.serviceitemid, serviceitem, function (data) {
                alert("更新成功！");
                window.location.reload();
            }, function (data) {
                alert("更新失败！");
            })
        }
        $scope.uploadLogo = function (file) {
            FileUpload.uploadFileToUrl(file).then(function (data) {
                $scope.serviceitem.logo = data;
                alert("上传成功！");
            });
        }
    }])

    .controller('ServiceItemAddCtrl', ['$scope', '$stateParams', 'ServiceItemService', 'service_list', 'FileUpload', function ($scope, $stateParams, ServiceItemService, service_list, FileUpload) {
        $scope.services = service_list;
        $scope.add = function (serviceitem) {
            ServiceItemService.insert(serviceitem, function (data) {
                alert("添加成功！");
                history.go(-1);
            }, function (data) {
                alert("添加失败！");
            })
        }
        $scope.uploadLogo = function (file) {
            FileUpload.uploadFileToUrl(file).then(function (data) {
                if (data == undefined) {
                    return;
                }
                $scope.serviceitem.logo = data;
                alert("上传成功！");
            });
        }
    }])
;


